//
//  TianXieYanZhengmaViewController.m
//  ZhiBoYuan
//
//  Created by coder_di on 2018/4/18.
//  Copyright © 2018年 apple. All rights reserved.
//

#import "TianXieYanZhengmaViewController.h"

@interface TianXieYanZhengmaViewController ()<UITextFieldDelegate>
@property (nonatomic, strong)UITextField *yanzhengTextfield;
@property (nonatomic, strong)UIButton *chongFaBtn;
@property (nonatomic, strong)UIButton *xiayibuBtn;
@property (nonatomic, assign)int codeNum;
@end

@implementation TianXieYanZhengmaViewController
- (void)viewDidLoad {
    [super viewDidLoad];
    
    _codeNum = 60;
    
    [self sendMs];
    
    self.view.backgroundColor = hui1Color;
        self.title = @"填写验证码";
    
    [self setUI];
}



-(void)sendMs{
    SGParameter *parameter  = [[SGParameter alloc] init];
    parameter.body = @{@"phone":_xindePhone,
                       @"type":@"backPwd"
                       };
    
    [SGHttpTool POST:[NSString stringWithFormat:@"%@g=Client&m=Register&a=getSmsCode",HTTPP] parameter:parameter sucess:^(id responseObject) {
        NSLog(@"%@",responseObject);
        
        if ([responseObject[@"status"] intValue] == 200) {
            [NSTimer scheduledTimerWithTimeInterval:1.0
                                             target:self
                                           selector:@selector(handleMaxShowTimer:)
                                           userInfo:nil
                                            repeats:YES];
        }
        
        
    } failure:^(NSError *error) {
        NSLog(@"%@",error);
        
    }];
    
}

#pragma mark  - 验证码倒计时
- (void)handleMaxShowTimer:(NSTimer *)timer{
    
    _codeNum--;
    [_chongFaBtn setTitle:[NSString stringWithFormat:@"重新获取(%ld)",(long)_codeNum]   forState:UIControlStateNormal];
    _chongFaBtn.userInteractionEnabled = NO;
    _chongFaBtn.backgroundColor = hui2Color;
    if (_codeNum == 0) {
        NSLog(@"验证码超时");
        //        _codeNum = @"fsdsa";
        [_chongFaBtn setTitle:@"重新获取" forState:UIControlStateNormal];
        [timer invalidate];
        timer = nil;
        _codeNum = 60;
        _chongFaBtn.userInteractionEnabled = YES;
        _chongFaBtn.backgroundColor = ZhuTiColor;
    }
    
}

-(void)setUI{
    UILabel *lable = [[UILabel alloc]initWithFrame:CGRectMake(0, 20 * KscreenHeight + NAVIGATION_BAR_HEIGHT, ScreenWidth, 20 * KscreenHeight)];
    lable.text = @"验证码将会发送到新的手机";
    lable.font = [UIFont systemFontOfSize:16 * KscreenWidth];
    lable.textAlignment = NSTextAlignmentCenter;
    [self.view addSubview:lable];
    
    UILabel *phoneNumber = [[UILabel alloc]initWithFrame:CGRectMake(0, 60 * KscreenHeight + NAVIGATION_BAR_HEIGHT, ScreenWidth, 30 * KscreenHeight)];
    phoneNumber.text = _xindePhone;
    phoneNumber.font = [UIFont systemFontOfSize:20 * KscreenWidth];
    phoneNumber.textAlignment = NSTextAlignmentCenter;
    [self.view addSubview:phoneNumber];
    
    UIView *view = [[UIView alloc]initWithFrame:CGRectMake(0, 110 * KscreenHeight + NAVIGATION_BAR_HEIGHT, ScreenWidth, 50 * KscreenHeight)];
    view.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:view];
    
    UILabel *yanzhengLable = [[UILabel alloc]initWithFrame:CGRectMake(20 * KscreenWidth, 10 * KscreenHeight,60 *  KscreenWidth, 30  * KscreenHeight)];
    yanzhengLable.text = @"验证码:";
    yanzhengLable.font = [UIFont systemFontOfSize:18];
    yanzhengLable.textColor = hui7Color;
    [view addSubview:yanzhengLable];
    
    _yanzhengTextfield = [[UITextField alloc]initWithFrame:CGRectMake(90 * KscreenWidth,15 * KscreenHeight, 150 * KscreenWidth, 20 * KscreenHeight)];
    _yanzhengTextfield.textAlignment = NSTextAlignmentLeft;
    _yanzhengTextfield.placeholder = @"请输入6位验证码";
    _yanzhengTextfield.font = [UIFont systemFontOfSize:18];
    _yanzhengTextfield.textColor = hui7Color;
    _yanzhengTextfield.delegate = self;
    [view addSubview:_yanzhengTextfield];
    
    _chongFaBtn = [UIButton buttonWithType:(UIButtonTypeCustom)];
    _chongFaBtn.frame = CGRectMake(ScreenWidth - 120 * KscreenWidth, 0, 118 * KscreenWidth, 50 * KscreenHeight);
    _chongFaBtn.backgroundColor = ZhuTiColor;
    _chongFaBtn.layer.masksToBounds = YES;
    _chongFaBtn.layer.cornerRadius = 8 * KscreenWidth;
    
    [_chongFaBtn setTitle:@"获取验证码" forState:0];
    
    [_chongFaBtn addTarget:self action:@selector(chongfaAction) forControlEvents:(UIControlEventTouchUpInside)];
    [view addSubview:_chongFaBtn];
    
    _xiayibuBtn = [UIButton buttonWithType:(UIButtonTypeCustom)];
    _xiayibuBtn.frame = CGRectMake(20 * KscreenWidth, 200 * KscreenHeight + NAVIGATION_BAR_HEIGHT, ScreenWidth - 40 * KscreenWidth, 50 * KscreenHeight);
    _xiayibuBtn.layer.masksToBounds = YES;
    _xiayibuBtn.layer.cornerRadius = 8 * KscreenWidth;
    
    _xiayibuBtn.backgroundColor = ZhuTiColor;
    [_xiayibuBtn setTitle:@"完成" forState:0];
    [_xiayibuBtn addTarget:self action:@selector(okAction) forControlEvents:(UIControlEventTouchUpInside)];
    
    [self.view addSubview:_xiayibuBtn];
}


- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
        _xiayibuBtn.userInteractionEnabled=YES;

    return YES;
}


-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
    [_yanzhengTextfield resignFirstResponder];
}

-(void)okAction{
    SGParameter *parameter  = [[SGParameter alloc] init];
    
//    NSLog(@"%@--%@--%@",_xindePhone,_yanzhengTextfield.text,_oldYanZhengMa);
    
    parameter.body = @{@"new_phone":_xindePhone,
                       @"new_code":_yanzhengTextfield.text,
                       @"old_code":_oldYanZhengMa
                       };
    
    [SGHttpTool POST:[NSString stringWithFormat:@"%@g=Client&m=User&a=changPhone",HTTPP] parameter:parameter sucess:^(id responseObject) {
        NSLog(@"%@",responseObject);
        
        if ([responseObject[@"status"] intValue] == 200) {
            [MBProgressHUD showTextOnly:self.view message:@"修改成功" delay:2.0];
            
            UserinfoModel *model = [ZYUserManager userInfo];
            model.phone = _xindePhone;
            [ZYUserManager saveUserInfo:model];
            
            
            
            UIViewController *viewCtl = self.navigationController.viewControllers[1];
            [self.navigationController popToViewController:viewCtl animated:YES];
            
        }else{
            
            [MBProgressHUD showTextOnly:self.view message:responseObject[@"msg"] delay:2.0];
        }
        
        
    } failure:^(NSError *error) {
        NSLog(@"%@",error);
        
    }];
    
}

-(void)chongfaAction{
    [self sendMs];
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    
}

@end
